﻿using IcsSEMCtrl.Com.Secs;
using IcsSEMCtrl.Service.Logger;
using Lifter.Device;
using Lifter.Utility;
using SEComEnabler.SEComStructure;
using System.Threading;

namespace TKT.Lifter.CIM.Lifter.HSMSMsg.TransferActionMsg
{
    public class S1F4
    {
        public static SXTransaction MakeTransactionSend(LifterSEM lifterSEM, SecsEventArgs eventArgs)
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 1;
            sXTransaction.Function = 4;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Selected Equipment Status Data.";
            Thread.Sleep(3000);
            SecsValue secsValue = eventArgs.SecsValue;
            //获取SVID的个数
            int nSVIDCount = secsValue.ItemValueCount();
            //如果SVID为空
            if (nSVIDCount <= 0)
            {
                LogHelper.loggerInfo("Reply S1F3 SVID Is Empty.");
                sXTransaction.WriteNode(SX.SECSFormat.L, 0, "", "", new int[0]);
                return sXTransaction;
            }
            LogHelper.loggerInfo("Reply S1F3 Report Make Start(S1F3 VID ALL 开始创建消息).");

            sXTransaction.WriteNode(SX.SECSFormat.L, nSVIDCount, "", "", new int[0]);
            //遍历所有的SVID
            for (int i = 0; i < nSVIDCount; i++)
            {
                string key = string.Format("SVID{0}", i + 1);
                string strSVID = secsValue.GetValue(key);
                if (strSVID != null)
                {
                    if (strSVID == "38")
                    {
                        sXTransaction.WriteNode(SX.SECSFormat.U2, 2, ((int)lifterSEM.HostControlState).ToString(), lifterSEM.HostControlState.ToString() + "-ControlState[38]", new int[0]);
                    }
                    else if (strSVID == "36")
                    {
                        lifterSEM.makeListS1F3alarmState(ref sXTransaction);
                    }
                    else if (strSVID == "9")
                    {
                        lifterSEM.makeListS1F3ShuttleState(ref sXTransaction);
                    }
                    else if (strSVID == "8")
                    {
                        lifterSEM.makeListS1F3PortState(ref sXTransaction);
                    }
                    else if (strSVID == "13")
                    {
                        lifterSEM.makeListS1F3EnhancedTransfer(ref sXTransaction);
                    }
                    else if (strSVID == "11")
                    {
                        lifterSEM.makeListS1F3EnhancedCarrier(ref sXTransaction);
                    }
                    else if (strSVID == "23")
                    {
                        sXTransaction.WriteNode(SX.SECSFormat.U2, 2, ((int)lifterSEM.SCStatemode).ToString(), lifterSEM.HostControlState.ToString() + "-SCState[38]", new int[0]);
                    }
                }
            }
            return sXTransaction;
        }
    }
}
